package analyser.node.v2;

import analyser.node.AbstractNodeAnalyser;
import kit.AnalyserIndexerKit;
import kit.LetterIndexer;
import model.v2.conf.MibObjectGroupNodeV2;
import util.common.CharType;
import util.common.MibKeys;

import java.util.ArrayList;
import java.util.List;

public class MibObjectGroupV2Analyser  extends AbstractNodeAnalyser<MibObjectGroupNodeV2> {
    private MibObjectGroupNodeV2 node2;

    public MibObjectGroupV2Analyser(List<String> txtList) {
        super(txtList);
        node2 = node = new MibObjectGroupNodeV2();
    }

    @Override
    protected void doAnalyse() {
        int state = 1;
        int next;
        int fail;
        String str = null;
        while (state != 99){
            switch (state){
                case 1: //节点名
                    state = matchNotNullValue("name", CharType.Letter, CharType.Number, CharType.Hyphen) ? 2 : -state;
                    break;
                case 2: //
                    state = kit.nextMatch(MibKeys.OBJECT_GROUP, 3, -state);
                    break;
                case 3: //OBJECTS{ OR EMPTY
                    if ((state = kit.nextMatch(MibKeys.OBJECTS, 4, 6)) == 4){
                        state = kit.nextMatch("{", 4, -state);
                        node2.objects = new ArrayList<>();
                    }
                    break;
                case 4: //
                    str = indexer.getNextIgnoreLeftSpace(CharType.Letter, CharType.Number);
                    if ((state = kit.notNull(str, 5, -state)) == 5){
                        node2.objects.add(str);
                    }
                    break;
                case 5: // , or {
                    if ((state = kit.nextMatch(",", 4, -5)) == -5){
                        state = kit.nextMatch("}", 6, -5);
                    }
                    break;
                case 6: //
                    state = matchEssentialKeyValue(MibKeys.STATUS, false, CharType.Letter) ? 7 : -state;
                    break;
                case 7: //
                    state = matchEssentialKeyValue(MibKeys.DESCRIPTION, true) ? 8 : -state;
                    break;
                case 8: //
                    state = matchOmissibleKeyValue(MibKeys.REFERENCE, true) ? 10 : -state;
                    break;
                case 10:
                    if ((state = kit.nextMatch(MibKeys.ASSIGNMENT, 99, -10)) == 99){
                        this.analyseLeaf();
                    }
                    break;
                default:
                    kit.ThrowAnalysisException("节点ObjectType解析失败。", state, str);
            }
        }
        return;
    }
}
